package com.wtcrazy.controller;

import com.wtcrazy.dao.ProductMapper;
import com.wtcrazy.entity.Product;
import com.wtcrazy.service.Impl.ProductServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductServiceImpl productServiceImpl;

    @PostMapping("/SaveProduct")
    public boolean saveProduct(@RequestBody Product product) {
        return productServiceImpl.saveProduct(product);
    }

    @GetMapping("/FindAll")
    public List<Product> FindAll() {
        return productServiceImpl.list();
    }

    @GetMapping("/Page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Product> data = productMapper.selectPage(pageNum, pageSize, name);
        Integer total = productMapper.selectTotal(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @ApiOperation("删除药品")
    @DeleteMapping("/DeleteProduct")//
    public String deleteProduct(@RequestParam String id) {
        return productServiceImpl.deleteProduct(id);
    }


}
